<html>
<head>
  <title>01基本工作流程</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="655"/>
<h1>01基本工作流程</h1>

<div>
<span><div><div><span style="font-size: 18px; color: rgb(0, 0, 128); font-weight: bold;">构建虚拟工作环境：</span></div><div><br/></div><div>sudo apt-get install python3</div><div>sudo apt-get install python3-pip</div><div>sudo pip3 install virtualenv</div><div style="margin-top: 1em; margin-bottom: 1em;">接着在虚拟环境中安装 dependencies 到虚拟环境如下:(您也可以跳过该部分，在步骤3中让 TensorLayer 自动安装dependencies)</div><div><br/></div><div>virtualenv env</div><div>env/bin/pip install matplotlib</div><div>env/bin/pip install numpy</div><div>env/bin/pip install scipy</div><div>env/bin/pip install scikit-image</div><div><br clear="none"/></div><div><br clear="none"/></div><div><span style="font-size: 18px; color: rgb(0, 0, 128); font-weight: bold;">一 准备数据</span></div><div>1、自己生产数据</div><div>    </div><ul><li>Constant Value Tensors</li></ul><div>               tf.zeros(shape, dtype=tf.float32, name=None)</div><div>               tf.zeros_like(tensor, dtype=None, name=None)</div><div>               tf.ones(shape, dtype=tf.float32, name=None)</div><div>               tf.ones_like(tensor, dtype=None, name=None)</div><div>               tf.fill(dims, value, name=None)</div><div>               tf.constant(value, dtype=None, shape=None, name='Const')</div><ul><li>Sequences</li></ul><div>               tf.linspace(start, stop, num, name=None)</div><div>               tf.range(start, limit, delta=1, name='range')</div><ul><li>Random Tensors</li></ul><div>               tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)</div><div>               tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)</div><div>               tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)</div><div>               tf.random_shuffle(value, seed=None, name=None)</div><div>               tf.set_random_seed(seed)</div><div>2、引入数据</div><div>3、从文件读取数据</div><div>4、处理数据</div><div><span style="font-size: 18px; color: rgb(0, 0, 128); font-weight: bold;">二 定义模型</span></div><div>1、生成矩阵</div><div>2、定义变量</div><div>     符号变量：用于描述可交互的操作单元</div><div>     占位符placeholder：占位符，可作输入量，权重值，偏置量</div><div>     比占位符placeholder更好的是Vairable： 一个Variable代表一个<span style="font-weight: bold;">可修改的张量</span>，存在在TensorFlow的用于描述交互性操作的图中。它们可以用于计算输入值，也可以在计算中被修改。对于各种机器学习应用，一般都会有模型参数，可以用Variable表示。</div><div><br/></div><div><br/></div><div>3、确定关系方程；模型方程</div><div>     softmax：y = tf.nn.softmax(tf.matmul(x,W) + b)</div><div>4、定义损失函数</div><div>     “交叉熵”（cross-entropy）:需要一个占位符输入真实值y_，</div><div>                                                  cross_entropy = -tf.reduce_sum(y_*tf.log(y))</div><div><br/></div><div><br/></div><div>5、确定训练步骤</div><div style="margin-left: 40px;">现在我们知道我们需要我们的模型做什么啦，用TensorFlow来训练它是非常容易的。</div><div style="margin-left: 40px;">因为TensorFlow拥有一张描述你各个计算单元的图，它可以<span style="color: rgb(28, 51, 135); font-weight: bold;">自动地使用反向传播算法(backpropagation algorithm)</span>来有效地确定你的变量是如何影响你想要最小化的那</div><div style="margin-left: 40px;">个成本值的。然后，<span style="color: rgb(28, 51, 135); font-weight: bold;">TensorFlow会用你选择的优化算法</span>来不断地修改变量以降低成本。</div><ul><li>train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)</li></ul><div>        <img src="01基本工作流程_files/Image.png" type="image/png" data-filename="Image.png" style="height: auto;"/></div><div><span style="font-size: 18px; color: rgb(0, 0, 128); font-weight: bold;">三 运行模型</span></div><div>1、搞一个sess</div><div>     sess = tf.Session()</div><div>     sess.run(init)</div><div>2、初始化所有tf变量</div><div>     init = tf.initialize_all_variables()</div><div>3、run训练</div><div>     </div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">for i in range(1000):</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">  batch_xs, batch_ys = mnist.train.next_batch(100)</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})</span></div><div><br/></div></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">#step 9 : train the model</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">    for i in range(50): # train the model 100 epochs,</span> <span style="font-family: Monaco;"><font style="font-size: 12pt; color: rgb(173, 0, 0);"><b>在同一个数据集上重复训练50次</b></font></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">        total_loss = 0</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">        for x, y in data:    </span><span style="font-family: Monaco;"><font style="font-size: 12pt; color: rgb(173, 0, 0);"><b>#在数据集中的每条数据都反向传播一次</b></font></span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">            # Session runs train_op and fetch values of loss</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">            _, l = sess.run([optimizer, loss], feed_dict={X: x, Y:y})</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">            total_loss += l</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">        print('Epoch {0}: {1}'.format(i, total_loss/n_samples))</span></div><div><span style="font-family: Monaco; font-size: 9pt; color: rgb(51, 51, 51);">    </span></div></div><div><br clear="none"/></div><div><span style="font-size: 18px; color: rgb(0, 0, 128); font-weight: bold;">四 返回结果</span></div><div>1、返回怎样的结果</div><div><br clear="none"/></div><div>2、评估模型</div><div>     </div><div style="box-sizing: border-box; padding: 8px; font-size: 12px; border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902);"><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">accuracy = tf.reduce_mean(tf.cast(correct_prediction, &quot;float&quot;))</span></div><div><span style="background-color: rgb(251, 250, 248); color: rgb(51, 51, 51); font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12px;">print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})</span></div></div><div><br/></div><div>        tf.argmax：它能给出某个tensor对象在<span style="color: rgb(106, 0, 129); font-weight: bold;">某一维上的其数据最大值</span>所在的索引值。由于标签向量是由0,1组成，因此最大值1所在的索引位置就是类别标签，比如tf.argmax(y,1)返回的是模型对于任一输入x预测到的标签值，而 tf.argmax(y_,1) 代表正确的标签，我们可以用 tf.equal 来检测我们的预测是否真实标签匹配(索引位置一样表示匹配)。</div><div><br clear="none"/></div><div><br clear="none"/></div><div><br clear="none"/></div><div><h4>1. 准备训练数据</h4><h4>2. 接受命令行参数</h4><h4>3. 定义神经网络模型</h4><h4>4. 使用不同的优化算法</h4><h4>5. Online learning与Continuous learning</h4><h4>6. 使用TensorBoard优化参数</h4><h4>7. 分布式TensorFlow应用</h4><h4>8. Cloud Machine Learning</h4><div><br clear="none"/></div></div><div><br/></div></div></span>
</div></body></html> 